<?php
namespace app\index\controller;

use app\index\model\appInfo as AppInfoModel;

use app\index\controller\Util;

class Wx extends Util
{
    public function test()
    {
        return "I am groot!";
    }
    public function getWxInfo($code)
    {
        //从数据库读取 appid, appsecret
        $appInfo = AppInfoModel::getById(1);
        $appId = $appInfo->appId;
        $appSecret = $appInfo->appSecret;
        
        //请求微信官方后台获取 openid
        $reqParam = array(
            "appid" => $appId,
            "secret" => $appSecret,
            "js_code" => $code,
            "grant_type" => "authorization_code"
        );
        $reqRes = $this->httpGet("https://api.weixin.qq.com/sns/jscode2session", $reqParam);
        return $reqRes;
    }
    public function getOpenId($code)
    {
        $wxInfo = $this->getWxInfo($code);
        $json = json_decode($wxInfo, true);
        return $json["openid"];
    }
}